package com.jshare.dao.Impl;
// default package

import java.sql.Timestamp;
import java.util.List;
import java.util.Set;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jshare.dao.TieziDAO;
import com.jshare.entity.Tiezi;

/**
 	* A data access object (DAO) providing persistence and search support for Tiezi entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .Tiezi
  * @author MyEclipse Persistence Tools 
 */

public class TieziDaoImpl extends HibernateDaoSupport implements TieziDAO  {
	     private static final Logger log = LoggerFactory.getLogger(TieziDaoImpl.class);
		protected void initDao() {
		//do nothing
	}
    
    /* (non-Javadoc)
	 * @see com.jshare.action.TieziDAO#save(com.jshare.entity.Tiezi)
	 */
    public void save(Tiezi transientInstance) {
        log.debug("saving Tiezi instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	/* (non-Javadoc)
	 * @see com.jshare.action.TieziDAO#delete(com.jshare.entity.Tiezi)
	 */
	public void delete(Tiezi persistentInstance) {
        log.debug("deleting Tiezi instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    /* (non-Javadoc)
	 * @see com.jshare.action.TieziDAO#findById(java.lang.Integer)
	 */
    public Tiezi findById( java.lang.Integer id) {
        log.debug("getting Tiezi instance with id: " + id);
        try {
            Tiezi instance = (Tiezi) getHibernateTemplate()
                    .get(Tiezi.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    /* (non-Javadoc)
	 * @see com.jshare.action.TieziDAO#findByExample(com.jshare.entity.Tiezi)
	 */
    public List findByExample(Tiezi instance) {
        log.debug("finding Tiezi instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    /* (non-Javadoc)
	 * @see com.jshare.action.TieziDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Tiezi instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Tiezi as model where model." 
         						+ propertyName + "= ?";
		 return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	/* (non-Javadoc)
	 * @see com.jshare.action.TieziDAO#findByToppic(java.lang.Object)
	 */
	public List findByToppic(Object toppic
	) {
		return findByProperty(TOPPIC, toppic
		);
	}
	
	/* (non-Javadoc)
	 * @see com.jshare.action.TieziDAO#findByTags(java.lang.Object)
	 */
	public List findByTags(Object tags
	) {
		return findByProperty(TAGS, tags
		);
	}
	

	/* (non-Javadoc)
	 * @see com.jshare.action.TieziDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all Tiezi instances");
		try {
			String queryString = "from Tiezi";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    /* (non-Javadoc)
	 * @see com.jshare.action.TieziDAO#merge(com.jshare.entity.Tiezi)
	 */
    public Tiezi merge(Tiezi detachedInstance) {
        log.debug("merging Tiezi instance");
        try {
            Tiezi result = (Tiezi) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    /* (non-Javadoc)
	 * @see com.jshare.action.TieziDAO#attachDirty(com.jshare.entity.Tiezi)
	 */
    public void attachDirty(Tiezi instance) {
        log.debug("attaching dirty Tiezi instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    /* (non-Javadoc)
	 * @see com.jshare.action.TieziDAO#attachClean(com.jshare.entity.Tiezi)
	 */
    public void attachClean(Tiezi instance) {
        log.debug("attaching clean Tiezi instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static TieziDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (TieziDAO) ctx.getBean("TieziDAO");
	}

	public List findByStartPos(int startPos,int pageSize) {
		Query query = this.getSession().createQuery("from Tiezi as tie");
		query.setFirstResult(startPos);
		query.setMaxResults(pageSize);
		return query.list();
	}
}